/*
 * Copyright 1994, Black Adder Research, Inc.  This source code may be
 * redistributed and modified with one restriction - do not claim that 
 * you wrote it.
 *
 * Black Adder Research, Inc.
 * 730 Norell Ave. North
 * Stillwater, MN  55082
 * 
 */

#import <appkit/appkit.h>
#import <math.h>
#import <strings.h>
#import <defaults/defaults.h>
#import "Controller.h"
#import "AVIView.h"
#import "MPEGView.h"
#import <sys/param.h>
#import <libc.h>

/* File extensions for video files */
const char *aviType = "avi";
const char *mpgType = "mpg";


@implementation Controller : Object


- openRequest:sender
{
    id		openReq;
    char	srcName[256];
    const char	*const fileTypes[] = {aviType, mpgType, NULL};  
    
    openReq = [OpenPanel new];
    if ([openReq runModalForTypes:fileTypes]) {
		strcpy(srcName,  [openReq filename]);
		[self openFile: srcName];
    }
    return self;
}


- (BOOL) openFile: (char *) srcName
{
    id  window, view=nil;
    NXRect	rect;
	char *ext;
	BOOL retVal = NO;
	
	/* determine file type and allocate appropriate view type */
	ext = rindex(srcName, '.');
	++ext;
	if(0 == strncmp(aviType, ext, 3)) {
		if([self getAVIFrameSize: &(rect.size) forFile: srcName]) {
			view = [[AVIView alloc] initSize: (const NXSize *) &(rect.size)];
		}
	}
	else {
		if([self getMPEGFrameSize: &(rect.size) forFile: srcName]) {
			view = [[MPEGView alloc] initSize: (const NXSize *) &(rect.size)];
		}
	}
	
	if (nil != view) {
		rect.origin.x = 0.0;
		rect.origin.y = 0.0;
		window = [[Window alloc] initContent:&rect
				style:NX_TITLEDSTYLE
				backing:NX_NONRETAINED
				buttonMask:NX_CLOSEBUTTONMASK
				defer:NO];
		[window setDelegate:self];
		[window setHideOnDeactivate: YES];
		[[window setContentView:view] free];
		[window center];
		[window setTitleAsFilename:srcName];
		[window makeKeyAndOrderFront:self];
		[window display];
		[view runFromFile: srcName];
		retVal = YES;
	}
    return(retVal);
}


- getAVIFrameSize: (NXSize *) fSize forFile: (char *) aviFile
{
	char command[256];
	id retVal = nil;
	int iTemp;
	FILE *ifp;

	if (strcmp(aviFile, "") != 0) {
		sprintf(command, "%s/aviDecode %s", [[NXBundle mainBundle] directory], aviFile);
		if ((ifp = popen(command, "r")) != NULL) {
			/* width and height are first 8 bytes (2 ints) in stream */
			fread(&iTemp, 1, sizeof(int), ifp);
			fSize->width = (float) iTemp;
			fread(&iTemp, 1, sizeof(int), ifp);
			fSize->height = (float) iTemp;
			pclose(ifp);
			retVal = self;
		}
	}
	return(retVal);
}


- getMPEGFrameSize: (NXSize *) fSize forFile: (char *) mpegFile
{
	char command[256];
	id retVal = nil;
	int iTemp;
	FILE *ifp;

	if (strcmp(mpegFile, "") != 0) {
		sprintf(command, "%s/mpegDecode %s", [[NXBundle mainBundle] directory], mpegFile);
		if ((ifp = popen(command, "r")) != NULL) {
			/* skip over frame number */
			fread(&iTemp, 1, sizeof(int), ifp);
			/* now get width and height */
			fread(&iTemp, 1, sizeof(int), ifp);
			fSize->width = (float) iTemp;
			fread(&iTemp, 1, sizeof(int), ifp);
			fSize->height = (float) iTemp;
			pclose(ifp);
			retVal = self;
		}
	}
	return(retVal);
}


- playAgain: sender
{
	id theWindow = [NXApp mainWindow];
	if([[theWindow contentView] respondsTo: @selector(runAgain)]) {
		[[theWindow contentView] runAgain];
	}
	return(self);
}


/* We always want to accept files. */
- (BOOL)appAcceptsAnotherFile:sender
{
	return (YES);
}


/* Open avi file from workspace. */
- (int) appOpenFile:(const char *)filename type:(const char *)aType
{
	BOOL retVal = NO;

	if((!strcmp(aType, aviType)) || (!strcmp(aType, mpgType))) {
		retVal = [self openFile:(char *)filename];
	}
	return (retVal);
}


@end
